<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set the value of an SNMP object</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="snmp.getnext.html">SNMP::getnext</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="snmp.setsecurity.html">SNMP::setSecurity</a></div>
 <div class="up"><a href="class.snmp.html">SNMP</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="snmp.set" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SNMP::set</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0)</p><p class="refpurpose"><span class="refname">SNMP::set</span> &mdash; <span class="dc-title">Set the value of an SNMP object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-snmp.set-description">
  <h3 class="title">说明</h3>

  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>SNMP::set</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$object_id</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$type</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>
   )</div>

  <p class="para rdfs-comment">
   Requests remote <acronym title="Simple Network Management Protocol">SNMP</acronym> agent setting the value of
   one or more SNMP objects specified by the <em><code class="parameter">object_id</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-snmp.set-parameters">
  <h3 class="title">参数</h3>
  <dl>

   <p class="para">
    If <em><code class="parameter">object_id</code></em> is <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, both
    <em><code class="parameter">type</code></em> and <em><code class="parameter">value</code></em> must be
    <span class="type"><a href="language.types.string.html" class="type string">string</a></span> too. If <em><code class="parameter">object_id</code></em> is <span class="type"><a href="language.types.array.html" class="type array">array</a></span>
    <em><code class="parameter">value</code></em> must be equal-sized array containing
    corresponding values, <em><code class="parameter">type</code></em> may be either
    <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (it&#039;s value will be used for all
    <em><code class="parameter">object_id</code></em>-<em><code class="parameter">value</code></em> pairs) or
    equal-sized array with per-OID value. When any other parameters&#039; combinations are
    used, a number of E_WARNING messages may be shown with detailed description.
   </p>
   
    <dt>
<em><code class="parameter">object_id</code></em></dt>

    <dd>

     <p class="para">
      The SNMP object id
     </p>
     <p class="para">
      When count of OIDs in object_id array is greater than
      max_oids object property set method will have to use multiple queries
      to perform requested value updates. In this case type and value checks
      are made per-chunk so second or subsequent requests may fail due to
      wrong type or value for OID requested. To mark this a warning is
      raised when count of OIDs in object_id array is greater than max_oids.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">type</code></em></dt>

    <dd>

     <p class="para">
<acronym>MIB</acronym> 定义了各个对象 id 的类型。必须是下面列出的单个字符之一。
</p>
<table class="doctable table">
 <caption><strong>类型</strong></caption>
 
  <tbody class="tbody">
   <tr><td>=</td><td>MIB类型</td></tr>

   <tr><td>i</td><td>INTEGER</td> </tr>

   <tr><td>u</td><td>INTEGER</td></tr>

   <tr><td>s</td><td>STRING</td></tr>

   <tr><td>x</td><td>HEX STRING</td></tr>

   <tr><td>d</td><td>DECIMAL STRING</td></tr>

   <tr><td>n</td><td>NULLOBJ</td></tr>

   <tr><td>o</td><td>OBJID</td></tr>

   <tr><td>t</td><td>TIMETICKS</td></tr>

   <tr><td>a</td><td>IPADDRESS</td></tr>

   <tr><td>b</td><td>BITS</td></tr>

  </tbody>
 
</table>

<p class="para">
 如果在编译 <acronym title="Simple Network Management Protocol">SNMP</acronym> 库时定义了 <strong><code>OPAQUE_SPECIAL_TYPES</code></strong>，那么下列值是合法的：
</p>
<table class="doctable table">
 <caption><strong>types</strong></caption>
 
  <tbody class="tbody">
   <tr><td>U</td><td>unsigned int64</td></tr>

   <tr><td>I</td><td>signed int64</td></tr>

   <tr><td>F</td><td>float</td></tr>

   <tr><td>D</td><td>double</td></tr>

  </tbody>
 
</table>


     <p class="para">
这些值大都会使用与 ASN.1 相符的类型。&#039;s&#039;，&#039;x&#039;，&#039;d&#039;
以及 &#039;b&#039; 都是指定一个八字节字符串值的方式。并且 &#039;u&#039; 无符号类型也可用于处理 Gauge32 值。
</p>

     <p class="para">
如果 MIB 文件是用 &quot;snmp_read_mib&quot; 或者通过在 libsnmp config 中指定而加载入 MIB 树时，
&#039;=&#039; 可以被用作为所有对象的 <em><code class="parameter">type</code></em> 参数，因为 type 可以被自动从 MIB 中读取。
</p>

     <p class="para">
注意有两种方式可以设定 BITS 类型的变量，例如
&quot;SYNTAX    BITS {telnet(0), ftp(1), http(2), icmp(3), snmp(4), ssh(5), https(6)}&quot;:
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <span class="simpara">
   使用 &quot;b&quot; 类型以及一个位数的列表。不推荐使用此方法，因为 GET 查询对同一个 OID 将会返回例如 0xF8。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   使用 &quot;x&quot; 类型以及一个十六进制数但是不需要通常的 &quot;0x&quot; 前缀。
  </span>
 </li>
</ul>
<p class="para">
 更多细节见范例部分。
</p>

    </dd>

   
   
    <dt>
<em><code class="parameter">value</code></em></dt>

    <dd>

     <p class="para">
      The new value.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-snmp.set-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-snmp.set-errors">
<h3 class="title">错误／异常</h3>
<p class="para">
 本方法默认不抛出任何异常。如果要在某些库出错时抛出 SNMPException 
 异常，用户需要将 SNMP 类参数 <em><code class="parameter">exceptions_enabled</code></em> 
 设定为相应的值。更多细节见
 <a href="class.snmp.html#snmp.props.exceptions-enabled" class="link">
  <em><code class="parameter">SNMP::$exceptions_enabled</code></em> 的解释</a>。
</p>
</div>



 <div class="refsect1 examples" id="refsect1-snmp.set-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="snmp.set.example.basic">
   <p><strong>Example #1 Set single <acronym title="Simple Network Management Protocol">SNMP</acronym> object id</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$session&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">(</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">::</span><span style="color: #0000BB">VERSION_2C</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"private"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'SNMPv2-MIB::sysContact.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'s'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Nobody"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="snmp.set.example.multiple">
   <p><strong>Example #2 Set multiple values using single <span class="methodname"><strong>SNMP::set()</strong></span>
   call</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$session&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">(</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">::</span><span style="color: #0000BB">VERSION_2C</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"private"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'SNMPv2-MIB::sysContact.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SNMPv2-MIB::sysLocation.0'</span><span style="color: #007700">),&nbsp;array(</span><span style="color: #DD0000">'s'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'s'</span><span style="color: #007700">),&nbsp;array(</span><span style="color: #DD0000">"Nobody"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Nowhere"</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//&nbsp;or<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'SNMPv2-MIB::sysContact.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SNMPv2-MIB::sysLocation.0'</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'s'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"Nobody"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Nowhere"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
   <div class="example" id="snmp.set.example.bits">
    <p><strong>Example #3 Using <span class="methodname"><strong>SNMP::set()</strong></span> for setting BITS <acronym title="Simple Network Management Protocol">SNMP</acronym> object id</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$session&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">(</span><span style="color: #0000BB">SNMP</span><span style="color: #007700">::</span><span style="color: #0000BB">VERSION_2C</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"private"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'FOO-MIB::bar.42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0&nbsp;1&nbsp;2&nbsp;3&nbsp;4'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;or<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'FOO-MIB::bar.42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'F0'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-snmp.set-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="snmp.get.html" class="methodname" rel="rdfs-seeAlso">SNMP::get()</a> - Fetch an SNMP object</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="snmp.getnext.html">SNMP::getnext</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="snmp.setsecurity.html">SNMP::setSecurity</a></div>
 <div class="up"><a href="class.snmp.html">SNMP</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
